package com.cmccgz.udacs_landlord_webapp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.bean.CardInfo;
import com.cmccgz.udacs_landlord_webapp.interfaces.OnRvItemClickListenter;
import com.cmccgz.udacs_landlord_webapp.update.StringUtils;

import java.util.List;

public class BindCardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    private List<CardInfo>datas;
    private OnRvItemClickListenter<CardInfo>listenter;

    public BindCardAdapter(Context mContext, List<CardInfo> datas, OnRvItemClickListenter<CardInfo> listenter) {
        this.mContext = mContext;
        this.datas = datas;
        this.listenter = listenter;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case -1:
                return new AddViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_card_add,parent,false));
            case 1:
                return new NormalViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_card_bind,parent,false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final CardInfo card = getCard(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listenter!=null) {
                    listenter.onItemClick(card,position,v);
                }
            }
       });
        if (getItemViewType(position)==1) {
            ((NormalViewHolder) holder).tv_card_num.setText(card==null||StringUtils.isEmpty(card.getUuid())?"":card.getUuid());
        }
    }
    private CardInfo getCard(int position){
        if (getItemViewType(position)==1) {
            return datas.get(position);
        }
        return null;
    }
    @Override
    public int getItemCount() {
        return (datas==null?0:datas.size())+1;
    }

    @Override
    public int getItemViewType(int position) {
        return position==getItemCount()-1?-1:1;
    }
    private class NormalViewHolder extends RecyclerView.ViewHolder{

        private final TextView tv_card_num;

        public NormalViewHolder(View itemView) {
            super(itemView);
            tv_card_num = ((TextView) itemView.findViewById(R.id.tv_card_num));
        }
    }
    private class AddViewHolder extends RecyclerView.ViewHolder{

        public AddViewHolder(View itemView) {
            super(itemView);
        }
    }
}
